Course Schedule III
Question
Given a list of courses, each represented by an integer start and end time, determine the maximum number of courses that can be taken concurrently.
Example 1
None
Solution
- ▭
- ▯
all//Course Schedule III.py
#Solution:
#This problem can be solved using a greedy approach.
#First, we sort the courses by their end time in increasing order.
#Then, we iterate through the courses one by one and check if the current course can be taken without exceeding the maximum number of courses.
#If it can, we add it to the list of taken courses and increment the count of taken courses.
#If it cannot, we skip it and move onto the next course.
#At the end of the iteration, we return the count of taken courses.
def courseScheduleIII(courses):
# Sort the courses by end time
courses.sort(key=lambda x: x[1])
# Initialize the count of taken courses to 0
taken = 0
# Iterate through the courses
for course in courses:
# Check if the current course can be taken
if taken + 1 <= course[1]:
# Increment the count of taken courses
taken += 1
# Return the count of taken courses
return taken
all//Course Schedule III.py
#Solution:
#This problem can be solved using a greedy approach.
#First, we sort the courses by their end time in increasing order.
#Then, we iterate through the courses one by one and check if the current course can be taken without exceeding the maximum number of courses.
#If it can, we add it to the list of taken courses and increment the count of taken courses.
#If it cannot, we skip it and move onto the next course.
#At the end of the iteration, we return the count of taken courses.
def courseScheduleIII(courses):
# Sort the courses by end time
courses.sort(key=lambda x: x[1])
# Initialize the count of taken courses to 0
taken = 0
# Iterate through the courses
for course in courses:
# Check if the current course can be taken
if taken + 1 <= course[1]:
# Increment the count of taken courses
taken += 1
# Return the count of taken courses
return taken